﻿using System;
using System.Collections.Generic;
using System.Web;
using NhanAi.Entity;
using Home.Utils;
using System.Data.OleDb;
namespace NhanAi.DAO
{
    public class SubscribingExtensionDao:DBConnection
    {
        public void Create(SubscribingExtension t)
        {
            NonQueryCommandBuilder nb = new NonQueryCommandBuilder("SubscribingExtension");
            try
            {
                if (Connect())
                {
                    nb.AddValueField("SubscribingId", t.SubscribingId);
                    nb.AddValueField("ExtKey", t.ExtKey);
                    nb.AddValueField("ExtValue", t.ExtValue);
                    nb.AddValueField("SortOrder", t.SortOrder);
                    OleDbCommand cmd = nb.GetCreateCommand(conn);

                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "select @@IDENTITY";
                    t.Id = (int)cmd.ExecuteScalar();
                    Disconnect();
                }
            }
            catch (Exception ex) { throw ex; }
        }
        public void Update(SubscribingExtension t)
        {
            NonQueryCommandBuilder nb = new NonQueryCommandBuilder("SubscribingExtension");
            try
            {
                if (Connect())
                {                              
                    nb.AddValueField("ExtValue", t.ExtValue);
                    nb.AddValueField("SortOrder", t.SortOrder);
                    nb.AddConditionField("Id", t.Id);
                    OleDbCommand cmd = nb.GetUpdateCommand(conn, true);
                    cmd.ExecuteNonQuery();

                    Disconnect();
                }
            }
            catch (Exception ex) { throw ex; }
        }
        public void Delete(int id)
        {
            NonQueryCommandBuilder nb = new NonQueryCommandBuilder("SubscribingExtension");
            try
            {
                if (Connect())
                {
                    nb.AddConditionField("Id", id);
                    OleDbCommand cmd = nb.GetUpdateCommand(conn, true);
                    cmd.ExecuteNonQuery();
                    Disconnect();
                }
            }
            catch (Exception ex) { throw ex; }
        }
        public void Delete(string ids)
        {
            OleDbCommand cmd = new OleDbCommand("delete from SubscribingExtension where Id in (" + ids + ")", conn);
            if (Connect())
            {
                cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        public List<SubscribingExtension> ReadByParent(int subsId)
        {
            OleDbCommand cmd = new OleDbCommand("select * from SubscribingExtension where SubscribingId=@id",conn);
            List<SubscribingExtension> list = new List<SubscribingExtension>();
            cmd.Parameters.AddWithValue("@id",subsId);
            if (Connect())
            {
                OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    SubscribingExtension e = new SubscribingExtension();
                    e.Id = dr.GetInt32(0);
                    e.SubscribingId = dr.GetInt32(1);
                    e.ExtKey = dr.GetString(2);
                    e.ExtValue = dr.GetString(3);
                    e.SortOrder = dr.GetInt16(4);
                    list.Add(e);
                }           

            }
            return list;
        }
    }
}